package com.leyu.mall.admin.goods.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.leyu.mall.admin.cms.service.ICmsChannelService;
import com.leyu.mall.admin.goods.domain.GoodsBase;
import com.leyu.mall.admin.goods.service.IGoodsBaseService;
import com.leyu.mall.admin.goods.service.IGoodsBrandService;
import com.leyu.mall.admin.goods.service.IGoodsCategoryService;
/**
 * 〈一句话功能简述〉 〈功能详细描述〉
 * @author OOKK
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
@Controller
@Scope(value = "prototype")
@RequestMapping("admin/goods")
public class GoodsBaseController
{
	@Resource(name = "goodsBaseServiceImpl")
	private IGoodsBaseService		goodsBaseServiceImpl;
	@Autowired
	private IGoodsBrandService		goodsBrandServiceImpl;
	@Autowired
	private IGoodsCategoryService	goodsCategoryService;
	@Autowired
	private ICmsChannelService		cmsChannelService;

	@RequestMapping(value = "save", method = RequestMethod.POST)
	public String save(Model model, GoodsBase goodsBase)
	{
		try
		{
			System.out.println("GoodsBaseController:save");
			goodsBaseServiceImpl.save(goodsBase);
		} catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "redirect:../goods";
	}

	@RequestMapping(value = "delete/{goodsID}", method = RequestMethod.GET)
	public String delete(Model model, @PathVariable long goodsID)
	{
		try
		{
			goodsBaseServiceImpl.deleteGoodsBase(goodsID);
		} catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "redirect:../../goods";
	}

	@RequestMapping(value = "edit/{goodsID}", method = RequestMethod.GET)
	public String edit(Model model, @PathVariable long goodsID)
	{
		try
		{
			model.addAttribute("entity", goodsBaseServiceImpl.getGoodsBaseById(goodsID));
			model.addAttribute("brandlist", goodsBrandServiceImpl.getAllDisplayGoodsBrand());
			model.addAttribute("categorylist", goodsCategoryService.getAllDisplayCategoryList());
			model.addAttribute("channellist", cmsChannelService.getcmsChannelList());
		} catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "admin/editgoods.html";
	}

	@ExceptionHandler(Exception.class)
	public String exception(Exception e, HttpServletRequest request)
	{
		request.setAttribute("exception", e);
		return "/error.html";
	}

	@RequestMapping(value = "new", method = RequestMethod.GET)
	public String newBrand(Model model) throws Exception
	{
		System.out.println("---------------");
		model.addAttribute("brandlist", goodsBrandServiceImpl.getAllDisplayGoodsBrand());
		model.addAttribute("categorylist", goodsCategoryService.getAllDisplayCategoryList());
		model.addAttribute("channellist", cmsChannelService.getcmsChannelList());
		return "admin/newgoods.html";
	}

	@RequestMapping(value = "", method = RequestMethod.GET)
	public String index(Model model) throws Exception
	{
		try
		{
			model.addAttribute("goodslist", goodsBaseServiceImpl.getAllGoodsBase());
		} catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "admin/goods.html";
	}
}
